//This is the implementation of the IController Interface.
function Controller(iview) {
//The Controller has a reference to the View, but only through the Interface (IView), not the actual implementation which could be interchangeable
  Controller.prototype.view = iview;
  Controller.prototype.pagingController = null;
  Controller.prototype.searchCriteria = null;
}

Controller.prototype = Object.create(IController.prototype);
//This function returns a new comparator function based on the sort criteria, this function is not the actual comparator function it only builds it
Controller.prototype.getArrayComparator = function(sortByType,sortAsc) {
	var comparatorFunc=function Comparator(a,b){		
		var contentA=a.content.resource;
		var contentB=b.content.resource;	
		if (contentA[sortByType].toLowerCase() < contentB[sortByType].toLowerCase()) return sortAsc? -1 : 1;
		if (contentA[sortByType].toLowerCase() > contentB[sortByType].toLowerCase()) return sortAsc? 1 : -1;
		return 0;
		}
	return comparatorFunc;
}


Controller.prototype.processRetreiveData=function(json) {
	var self=this;
	var innerResultArray=json.response.results.result;
	if (this.searchCriteria.searchText!=''){
		var filteredArray=$.grep(innerResultArray, function( n, i ) {	
			var innerResource=n.content.resource;
			return innerResource.resource_type.toLowerCase().indexOf(self.searchCriteria.searchText.toLowerCase()) >= 0 ||
			 innerResource.display_title.toLowerCase().indexOf(self.searchCriteria.searchText.toLowerCase()) >= 0 ||
			  innerResource.media_type.toLowerCase().indexOf(self.searchCriteria.searchText.toLowerCase()) >= 0 ||
			  innerResource.language.toLowerCase().indexOf(self.searchCriteria.searchText.toLowerCase()) >= 0;
		});
	}
	else{
		var filteredArray=innerResultArray;
	}
	
	var sortedArray= filteredArray.sort(this.getArrayComparator(this.searchCriteria.sortType,this.searchCriteria.sortASC));
	//Transforms the resources array into the element list thet IView expects
	var contentAllViewArray = $.map( sortedArray, function( n, i ) {
		var innerResource = n.content.resource;
		var viewableFlag=innerResource.viewable=="true"?true:false;
		return {
			rowIndex:i,
			id : innerResource.id,
			resourceType:innerResource.resource_type,
			displayTitle:innerResource.display_title,
			mediaType:innerResource.media_type,
			language:innerResource.language,
			mediaTypeIconUrl:self.getMediaTypeIconURL(innerResource.media_type_id),
			viewable:viewableFlag,
			viewableIcon:viewableFlag?"Content/yes.png":"Content/no.png"
		};
	});
	//If this condition is not hit it means a previous or next page was selected
	if (this.isNewSearch()){
		//If this condition is hit, It means that it's a new search, new total records are expected and the paging controller needs to be restored to its defaults.
		this.pagingController=new PagingController(contentAllViewArray.length);
		//now if I evaluate this.isNewSearch() is false
	}
	
	var contentViewArray=$.grep(contentAllViewArray, function( n, i ) {		
	
		return self.pagingController.getCurrentPageStartIndex()<=n.rowIndex && n.rowIndex<=self.pagingController.getCurrentPageEndIndex()
	});
	
	this.view.bindData(contentViewArray);
	this.view.setNavigationButtonsAvailability(new NavigationButtonsModel(this.pagingController.previousEnabled(),this.pagingController.nextEnabled(),this.pagingController.GetPageDescription()));
}

//This is a proper way to retreive the Json file from a Web Server
/*
Controller.prototype.retreiveData=function(){
var self = this;
$.getJSON( "Content.json", function( json) {	
	self.processRetreiveData(json);
	});
}*/

//Invoking the global getHardCodedJsonContent() function located at 'File:\\\HardCodedJsonContent.js', to get the json content as a workaround to the security policies in IE and Chrome when loading local files.
Controller.prototype.retreiveData=function(){
	this.processRetreiveData(getHardCodedJsonContent());
}

Controller.prototype.getMediaTypeIconURL = function(mediaTypeId) {	
	switch (mediaTypeId)
	{
	   case "1":
			return "Content/flash.png";
	   case "4":
			return "Content/pdf.gif";
	   case "2":
			return "Content/html.gif";
	   default: 
		   throw new Error ("Not implemented");
		   break;
	}
}

Controller.prototype.isNewSearch=function(){
	return this.pagingController==null;
}
//This functin is only called by the IController interface
Controller.prototype.fetchData = function(searchCriteria) {	
	this.searchCriteria=searchCriteria;	
	this.pagingController=null;
	//now if I evaluate self.isNewSearch() is true
	this.retreiveData();
}

Controller.prototype.moveNext = function() {
	this.pagingController.moveNext();
	this.retreiveData();
}
Controller.prototype.movePrevious = function() {
	this.pagingController.movePrevious();
	this.retreiveData();
}

//This is a proper way to retreive the Json file from a Web Server
/*
Controller.prototype.getAdditionalData = function (resourceId) {
	var self = this;
	$.getJSON( "Content.json", function( json) {	
		self.processGetAdditionalData(json,resourceId);
	});
};*/

//Invoking the global getHardCodedJsonContent() function located at 'File:\\\HardCodedJsonContent.js', to get the json content as a workaround to the security policies in IE and Chrome when loading local files.
Controller.prototype.getAdditionalData = function (resourceId) {
	this.processGetAdditionalData(getHardCodedJsonContent(),resourceId);
};

Controller.prototype.processGetAdditionalData = function (json,resourceId){
	var innerResultArray=json.response.results.result;
	var filteredArray=$.grep(innerResultArray, function( n, i ) {		
		return 	 n.content.resource.id==resourceId;
	});
	var firstElement=filteredArray[0];
	var additionalDataModel={
		id : firstElement.content.resource.id,
		meaningfulDescription:firstElement.content.resource.meaningful_description,
		additionalText:firstElement.content.resource.additional_text,
		categorization:firstElement.content.resource.categorization,
		standards:firstElement.content.resource.standards?
			$.map( firstElement.content.resource.standards.standard, function( n, i ) {
				return n.description;
			})
			:null
	};
	this.view.bindAdditionalDetailsData(additionalDataModel);	
}

Controller.prototype.processGetFullDetails = function (json,resourceId) {
	var innerResultArray=json.response.results.result;
	var filteredArray=$.grep(innerResultArray, function( n, i ) {		
		return 	 n.content.resource.id==resourceId;
	});
	var firstElement=filteredArray[0];
	var additionalDataModel={					
		
		id : firstElement.content.resource.id,
		resourceType:firstElement.content.resource.resource_type,
		displayTitle:firstElement.content.resource.display_title,
		mediaType:firstElement.content.resource.media_type,
		language:firstElement.content.resource.language,
		meaningfulDescription:firstElement.content.resource.meaningful_description,
		additionalText:firstElement.content.resource.additional_text,
		categorization:firstElement.content.resource.categorization,
		standards:firstElement.content.resource.standards?
			$.map( firstElement.content.resource.standards.standard, function( n, i ) {
				return n.description;
			})
			:null,
		GUID:firstElement.content.resource.GUID
	};
	this.view.bindFullDetailsData(additionalDataModel);	
}

//This is a proper way to retreive the Json file from a Web Server
/*
Controller.prototype.getFullDetails = function (resourceId) {
	var self = this;
	$.getJSON( "Content.json", function(json) {	
		self.processGetFullDetails(json,resourceId);
	});
};*/

//Invoking the global getHardCodedJsonContent() function located at 'File:\\\HardCodedJsonContent.js', to get the json content as a workaround to the security policies in IE and Chrome when loading local files.
Controller.prototype.getFullDetails = function (resourceId) {
	this.processGetFullDetails(getHardCodedJsonContent(),resourceId);
};







